package com.huike.web.controller.business;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.service.TbBusinessService;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.AssignmentVo;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.PageAjaxResult;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.common.core.page.BusinessPageQueryDto;
import com.huike.common.core.page.OperLogPageQueryDto;
import com.huike.common.core.page.PageResult;
import com.huike.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api("商机列表")
@Slf4j
@RequestMapping("/business")
@RestController
public class TbBusinessController extends BaseController {
    @Resource
    private TbBusinessService businessService;

//    @ApiOperation("查询商机列表")0
//    @PreAuthorize("@ss.hasPermi('system:user:list')")
//    @GetMapping("/list")
//    public PageAjaxResult list(BusinessPageQueryDto dto){
//        log.info("查询商机列表，{}",dto);
//        PageResult pageResult = businessService.selectList(dto);
//        PageAjaxResult pageAjaxResult = PageAjaxResult.success();
//        pageAjaxResult.setRows(pageResult.getRecords());
//        pageAjaxResult.setTotal(pageResult.getTotal());
//        return pageAjaxResult;
//    }

    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbBusiness user) {
        startPage();
        log.info("存入的对象：{}", user);
        List<TbBusiness> list = businessService.selectUserList(user);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/pool")
    public TableDataInfo pool(TbBusiness user) {
        startPage();
        log.info("存入的对象：{}", user);
        List<TbBusiness> list = businessService.selectListPool(user);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @DeleteMapping("/{ids}")
    public AjaxResult deletedList(@PathVariable List<Long> ids) {
        businessService.deletedList(ids);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/{id}")
    public AjaxResult<TbBusiness> findDetalisById(@PathVariable Long id) {
        TbBusiness tbBusiness = businessService.findDetalisById(id);
        return AjaxResult.success(tbBusiness);
    }

    @ApiOperation("新增商机")
    @PostMapping
    public AjaxResult addTbBusiness(@RequestBody TbBusiness tbBusiness) {
        log.info("新增商机:{}", tbBusiness);
        return AjaxResult.success(businessService.addTbBusiness(tbBusiness));
    }


    @ApiOperation("修改商机")
    @PutMapping
    public AjaxResult updateTbBusiness(@RequestBody TbBusiness tbBusiness) {
        log.info("新增商机:{}", tbBusiness);
        return AjaxResult.success(businessService.updateTbBusiness(tbBusiness));
    }


    @ApiOperation("商机分配")
    @PutMapping("/assignment")
    public AjaxResult assignmentTbBusiness(@RequestBody AssignmentVo assignmentVo) {
        log.info("商机分配:{}", assignmentVo);
        return AjaxResult.success(businessService.assignmentTbBusiness(assignmentVo));
    }


    @ApiOperation("踢回公海")
    @PutMapping("/back/{id}/{reason}")
    public AjaxResult businessBack(@PathVariable Integer id, @PathVariable String reason) {
        log.info("踢回公海:{},{}", id, reason);
        return AjaxResult.success(businessService.businessBack(id, reason));
    }

    @ApiOperation("批量捞取")
    @PutMapping("/gain")
    public AjaxResult gain(@RequestBody AssignmentVo assignmentVo) {
        log.info("批量捞取:{}");
        return AjaxResult.success(businessService.gain(assignmentVo.getIds(), assignmentVo.getUserId()));
    }

    @PostMapping("/record")
    @ApiOperation("新增商机跟进记录")
    public AjaxResult followAdded(@RequestBody BusinessTrackVo businessTrackVo){
        businessService.followAdded(businessTrackVo);
        return AjaxResult.success("添加成功");
    }

    /**
     * 查询商机跟进记录列表
     * @return
     */
    @ApiOperation("查询商机跟进记录列表")
    @GetMapping("/record/list")
    public AjaxResult followSelect(Long id){
        List<TbBusinessTrackRecord> list = businessService.followSelect(id);
        return AjaxResult.success(list);
    }

}
